home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 12 code / Components / Sources / MoMathComponent.c < prev    next >
Encoding:
Text File  |  1994-12-11  |  7.4 KB  |  272 lines  |  [TEXT/MMCC]

  1. /************************************************************************************
  2.  
  3.     File:        MoMathComponent.c
  4.  
  5.     Contains:    Extended math component routines.
  6.  
  7.     Written by:    Gary Woodcock
  8.  
  9.     Copyright:    © 1992 by Apple Computer, Inc.
  10.  
  11.     Change History (most recent first):
  12.  
  13.             
  14.     Project settings for 68K
  15.         Code model:                 Small, Smart or Large
  16.         Link Single Segment:        on (VERY important)
  17.         Project type:                Code Resource
  18.         File name:                    MoMath_Component
  19.         Sym name:                    MoMath_Component.SYM (not generated)
  20.         Resource name:                Extended Math Component
  21.         Header type:                Standard
  22.         Multi Segment:                 on or off
  23.         ResType:                    _68K
  24.         ID:                            130 
  25.         SegType                        blank (unused since we are linking single segment)
  26.         Creator:                    gwck
  27.         File type:                    thng
  28.         Resource flags:                SysHeap
  29.         Project setup:
  30.             Segment 1 (Main Segment): MoMathComponent.c
  31.             Segment 2 (Group2): MacOS.lib, FatMoMathComponent.rsrc (See warning below)
  32.             Segment 3 (Group3): and MathComponentCommon.c
  33.                         
  34.     Project settings for PPC 
  35.         Project type:                Code Resource
  36.         File name:                    MoMath_Component
  37.         Sym name:                    MoMath_Component.SYM (not generated)
  38.         Resource name:                Extended Math Component
  39.         Header type:                None (VERY important)
  40.         ResType:                    _PPc
  41.         ID:                            130 
  42.         Creator:                    gwck
  43.         File type:                    thng
  44.         Resource flags:                SysHeap
  45.         Merge To File                on
  46.         Expand Uninitialized Data:     on
  47.         Main Entry Point            MainRD (VERY important)
  48.         Project setup:
  49.             Group 1 (Main Group): MoMathComponent.c
  50.             Group 2 (Group2): InterfaceLib, MathLib, ToolsLib.o
  51.             Group 3 (Group3): MathComponentCommon.c
  52.                         
  53.     ***Warning*** FatMoMathComponent.rsrc has an extended thng resource which 
  54.     is set to require both PPC and 68K code.  If you want to test the 68K 
  55.     component by itself, use MoMathComponent.rsrc, instead.
  56.  
  57.     CW components need to have the Link Single Segment switch checked.  The Multi-
  58.     segment switch can be checked or unchecked as fits your needs.  (Please 
  59.     see the User Guide for which situations require the Multi-segment switch.)
  60.     Putting everything into one segment eliminates segment loader errors that 
  61.     would occur if the startup code tried to call GetResource before 
  62.     OpenComponentResFile is called.  Components written in C are moveable, components 
  63.     written in C++ with virtual functions probably aren't.  Please see the 
  64.     C++ XCMD example on the CD for an example of how to deal C++ code resources.
  65.  
  66.     Project settings, port to PPC and comments by
  67.     Mark Anderson
  68.     metrowerks
  69.     12/1/94
  70.     
  71. ************************************************************************************/
  72.  
  73. //-----------------------------------------------------------------------
  74. // Includes
  75.  
  76. #include "MathComponent.h"
  77. #include "MathComponentPrivate.h"
  78. #include "MathComponentCommon.h"
  79. #include "MoMathComponent.h"
  80. #include "MoMathComponentPrivate.h"
  81. #include <Errors.h>
  82. #ifndef powerc
  83.     #include <A4Stuff.h>
  84. #endif
  85.  
  86. //-----------------------------------------------------------------------
  87. // Prototype (can't be in header or there is a conflict with Component Tester)
  88. #ifndef DEBUG_IT
  89.     pascal    ComponentResult    main    (ComponentParameters    *params,
  90.                                  Handle                    storage);
  91. #endif
  92.  
  93. //-----------------------------------------------------------------------
  94. // PPC Globals
  95. #ifdef powerc 
  96.  
  97.     INSTANTIATE_ROUTINE_DESCRIPTOR(MoMathDoAdd);
  98.     INSTANTIATE_ROUTINE_DESCRIPTOR(MoMathCanDo);
  99.  
  100. #ifndef DEBUG_IT     // This file must occur after MathComponent.c in the project window
  101.                     // otherwise these globals get defined twice
  102.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathOpen);
  103.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathClose);
  104.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathVersion);
  105.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathTarget);
  106.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathDoDivide);
  107.     INSTANTIATE_ROUTINE_DESCRIPTOR(MathDoMultiply);
  108.  
  109.     RoutineDescriptor MainRD = BUILD_ROUTINE_DESCRIPTOR(uppComponentRoutineProcInfo, main);
  110.     ProcInfoType __procinfo = uppComponentRoutineProcInfo;
  111. #else
  112.     extern RoutineDescriptor MathOpenRD;
  113.     extern RoutineDescriptor MathCloseRD;
  114.     extern RoutineDescriptor MathVersionRD;
  115.     extern RoutineDescriptor MathTargetRD;
  116.     extern RoutineDescriptor MathDoDivideRD;
  117.     extern RoutineDescriptor MathDoMultiplyRD;
  118. #endif
  119. #endif
  120.  
  121. //-----------------------------------------------------------------------
  122. #ifdef DEBUG_IT
  123.  
  124. // Use this declaration when we're running linked (for debugging)
  125. pascal    ComponentResult    MoMathDispatcher    (ComponentParameters    *params,
  126.                                              Handle                    storage)
  127.                                              
  128. #else
  129.  
  130. // Use this declaration when we're a standalone component
  131. pascal    ComponentResult    main    (ComponentParameters    *params,
  132.                                  Handle                    storage)
  133.  
  134. #endif DEBUG_IT
  135.  
  136. {
  137.     // This routine is the main dispatcher for the MoMath component
  138.     
  139.     ComponentResult            result = noErr;
  140.  
  141. #if !defined(powerc) && !defined(DEBUG_IT)
  142.     long                     oldA4;
  143.     
  144.     oldA4 = SetCurrentA4();
  145. #endif
  146.  
  147.  
  148.     // Did we get a Component Manager request code (< 0)?
  149.     if (params->what < 0)
  150.     {
  151.         switch (params->what)
  152.         {
  153.             case kComponentOpenSelect:        // Open request
  154.             {
  155.                 result = CallComponentFunctionUniv(params, MathOpen);
  156.                 break;
  157.             }    
  158.     
  159.             case kComponentCloseSelect:        // Close request
  160.             {
  161.                 //if open fails, close gets called so 
  162.                 //globals aren't allocated
  163.                 if (!storage)
  164.                     return result;
  165.                 result = CallComponentFunctionWithStorageUniv(storage, params, MathClose);
  166.                 break;    
  167.             }        
  168.             case kComponentCanDoSelect:        // Can Do request
  169.             {
  170.                 result = CallComponentFunctionUniv(params, MoMathCanDo);
  171.                 break;    
  172.             }
  173.             case kComponentVersionSelect:    // Version request
  174.             {
  175.                 result = CallComponentFunctionUniv(params, MathVersion);
  176.                 break;    
  177.             }    
  178.             case kComponentTargetSelect:    // Target request
  179.             {
  180.                 result = CallComponentFunctionWithStorageUniv(storage, params, MathTarget);
  181.                 break;    
  182.             }
  183.             case kComponentRegisterSelect:    // Register request not supported
  184.             default:                        // Unknown request
  185.             {
  186.                 result = paramErr;
  187.                 break;
  188.             }
  189.         }
  190.             
  191.         
  192.     }
  193.     else    // Was it one of our request codes?
  194.     {
  195.         switch (params->what)
  196.         {
  197.             // math component selectors
  198.             case kDoDivideSelect:            // Divide request
  199.             {
  200.                 result = CallComponentFunctionUniv(params, MathDoDivide);
  201.                 break;    
  202.             }    
  203.             case kDoMultiplySelect:            // Multiply request
  204.             {
  205.                 result = CallComponentFunctionUniv(params, MathDoMultiply);
  206.                 break;    
  207.             }
  208.             case kDoAddSelect:                // Add request
  209.             {
  210.                 result = CallComponentFunctionUniv(params, MoMathDoAdd);
  211.                 break;    
  212.             }
  213.             default:                        // Unknown request
  214.             {
  215.                 result = paramErr;
  216.                 break;
  217.             }
  218.  
  219.         }
  220.     }
  221.     
  222.  
  223. #if !defined(powerc) && !defined(DEBUG_IT)
  224.     SetA4(oldA4);
  225. #endif
  226.     return (result);
  227. }
  228.                                              
  229. //-----------------------------------------------------------------------
  230.  
  231. pascal    ComponentResult    MoMathCanDo    (short    selector)
  232. {
  233.     switch (selector)
  234.     {
  235.         // Component Manager request codes
  236.         case kComponentOpenSelect:
  237.         case kComponentCloseSelect:
  238.         case kComponentCanDoSelect:
  239.         case kComponentVersionSelect:
  240.         case kComponentTargetSelect:
  241.         
  242.         // Math component request codes
  243.         case kDoDivideSelect:
  244.         case kDoMultiplySelect:
  245.     
  246.         // MoMath component request codes
  247.         case kDoAddSelect:
  248.         {
  249.             return (true);
  250.         }
  251.         case kComponentRegisterSelect:    // Register request not supported
  252.         default:                        // Not a request code we recognize
  253.         {
  254.             return (false);
  255.         }
  256.     }
  257. }
  258.  
  259. //-----------------------------------------------------------------------
  260.  
  261. pascal    ComponentResult    MoMathDoAdd    (short    firstNum,
  262.                                          short    secondNum,
  263.                                          short    *additionResult)
  264. {
  265.     ComponentResult    result = noErr;
  266.     
  267.     *additionResult = firstNum + secondNum;
  268.  
  269.     return (result);
  270. }
  271.  
  272.